<?php
namespace app\admin\controller;

use app\common\model\project_model\ArticleRecordModel;
use app\common\model\WechatAppletModel;
use app\yxwadmin\controller\Admin;
use app\admin\model\validate\ArticleRecord as validateArticle;

class ArticleRecord extends Admin
{

    public $error_msg = '';

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        // 小程序
        $appletM = new WechatAppletModel();
        $appletD = $appletM->field('id, name')->where(['product_code' => 'photo_albums_bless'])->select();
        $this->assign('applet_list', $appletD);
        return $this->view->fetch();
    }

    /**
     * 初始化表格
     * 关键字搜索
     * 分页
     * @throws
     * @return mixed
     */
    public function getList()
    {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keyword = input('keywords','');
        $applet_app_id = input('applet_app_id', '');
        if ($applet_app_id) {
            $where['applet_app_id'] = $applet_app_id;
        } else {
            $where = '1=1';
        }
        $model = new ArticleRecordModel();
        if ($keyword) {
            $count = $model->where($where)->where('cate_name|token|title|button_name|button_key|lyric|share_title|content', 'like', '%' . $keyword . '%')->count();
            $list = $model
                ->where($where)
                ->where('cate_name|token|title|button_name|button_key|lyric|share_title|content', 'like', '%' . $keyword . '%')
                ->order([$sortName => $sortOrder])
                ->limit($offset . ',' . $limit)
                ->select();
        } else {
            $count = $model->where($where)->count(); //查询满足要求的总记录数
            $list = $model
                ->where($where)
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    /*public function view() {
        $id = input('id/d');
        $model = new ArticleRecordModel();
        $Rs = $model->where(['id' => $id])->find();
        $this->assign('Rs', $Rs);
        return $this->fetch();
    }*/

}